package com.zmzncs.lmtc.module.park.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zmzncs.lmtc.common.util.DateTimeUtil;
import com.zmzncs.lmtc.common.util.ExceptionUtil;
import com.zmzncs.lmtc.module.order.dao.ParkReserveRecordDao;
import com.zmzncs.lmtc.module.order.pojo.order_reserve.ParkReserveRecordDTO;
import com.zmzncs.lmtc.module.park.pojo.parkinfo.ParkInfo;
import com.zmzncs.lmtc.module.park.dao.ParkInfoDao;
import com.zmzncs.lmtc.module.park.pojo.parkinfo.ParkInfoDTO;
import com.zmzncs.lmtc.module.park.service.ParkInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zmzncs.lmtc.module.promotion.dao.ReserveTemporaryRuleDao;
import com.zmzncs.lmtc.module.promotion.pojo.Reserve_temporary_rule.ReserveTemporaryRuleDTO;
import com.zmzncs.lmtc.module.promotion.service.MonthRuleFeeScaleService;
import com.zmzncs.lmtc.module.shop.dao.ParkTicketSetParkMapper;
import com.zmzncs.lmtc.module.shop.pojo.park_ticket_set_park.ParkTicketSetParkDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 任建波
 * @since 2020-10-09
 */
@Service
@Slf4j
public class ParkInfoServiceImpl extends ServiceImpl<ParkInfoDao, ParkInfo> implements ParkInfoService {

    @Autowired
    ParkInfoDao parkInfoDao;
    @Autowired
    MonthRuleFeeScaleService monthRuleFeeScaleService;
    @Autowired
    ParkReserveRecordDao parkReserveRecordDao;
    @Autowired
    ParkTicketSetParkMapper parkTicketSetParkMapper;

    @Autowired
    ReserveTemporaryRuleDao reserveTemporaryRuleDao;

    /**
     * 获取附近停车场
     */
    @Override
    public IPage<ParkInfoDTO> getNearbyParkList(Page page, String longitude, String latitude) {
        IPage<ParkInfoDTO> iPage = parkInfoDao.getNearbyParkList(page, longitude, latitude);
        if (iPage != null && iPage.getRecords() != null && iPage.getRecords().size() > 0) {
            for (ParkInfoDTO dto : iPage.getRecords()) {
                Integer reserveParkSpace = dto.getReserveParkSpace();
                if (reserveParkSpace > 0) {
                    //根据车场Id统计预约通过数量
                    Long recordCount = parkReserveRecordDao.getRecordCountByParkId(dto.getId());
                    int count = reserveParkSpace - recordCount.intValue();
                    count = count < 0 ? 0 : count;
                    dto.setReserveParkSpace(count);
                }

                //判断是否有停车券
                List<ParkTicketSetParkDTO> setParkDTOS = parkTicketSetParkMapper.getByParkId(dto.getId());
                if (setParkDTOS != null && setParkDTOS.size() > 0) {
                    dto.setHasParkTicket(1);
                } else {
                    dto.setHasParkTicket(0);
                }
            }
        }

        return iPage;
    }

    /**
     * 查询附近有月租规则的停车场
     */
    @Override
    public IPage<ParkInfoDTO> getNearbyParkHasMonthRuleList(Page page, String longitude, String latitude) {
        IPage<ParkInfoDTO> iPage = parkInfoDao.getNearbyParkHasMonthRuleList(page, longitude, latitude);

        return iPage;
    }

    /**
     * 获取停车场列表
     */
    @Override
    public List<ParkInfoDTO> getParkList() {
        List<ParkInfoDTO> parkInfoDTOList = parkInfoDao.getParkList();

        return parkInfoDTOList;
    }

    /**
     * 获取预约停车场列表
     *
     * @param parkName
     */
    @Override
    public List<ParkInfoDTO> getReserveParkList(String parkName) {
        return parkInfoDao.getReserveParkList(parkName);
    }

    /**
     * 获取最近预约停车场列表
     *
     * @param longitude
     * @param latitude
     */
    @Override
    public ParkInfoDTO getNearbyReserveParkList(String longitude, String latitude) {
        return parkInfoDao.getNearbyReserveParkList(longitude, latitude);
    }

    @Override
    public List<ParkInfoDTO> getParkByName(String keyword) {
        return parkInfoDao.getParkByName(keyword);
    }


    @Override
    public Integer getParkReservesSpace(Long parkId, Date reserveDate) {
        Integer count = 0;
        ParkInfo parkInfo = parkInfoDao.selectById(parkId);
        //查询停车场*天预约成功的预约记录
        List<ParkReserveRecordDTO> recordDTOList = parkReserveRecordDao.getReserveRecordByReserveDate(parkId, reserveDate);
        //临时可预约
        ReserveTemporaryRuleDTO reserveTemporaryRuleDTO = reserveTemporaryRuleDao.getByDate(parkId, DateTimeUtil.convertYMD(reserveDate));

        if (reserveTemporaryRuleDTO != null && reserveTemporaryRuleDTO.getReserveSpace() > 0) {
            count = reserveTemporaryRuleDTO.getReserveSpace() - recordDTOList.size();
            count = count < 0 ? 0 : count;
        } else if (reserveTemporaryRuleDTO == null && parkInfo != null && parkInfo.getReserveParkSpace() > 0) {
            count = parkInfo.getReserveParkSpace();
            if (recordDTOList != null && recordDTOList.size() > 0) {
                count = count - recordDTOList.size();
                count = count < 0 ? 0 : count;
            }
        }
        return count;
    }

    @Override
    public ParkInfoDTO getParkInfoById(Long parkId) {
        ParkInfoDTO parkInfoDTO = parkInfoDao.getParkInfoById(parkId);
        if (parkInfoDTO != null) {
            Integer count = parkInfoDTO.getReserveParkSpace();
            //临时可预约
            ReserveTemporaryRuleDTO reserveTemporaryRuleDTO = reserveTemporaryRuleDao.getByDate(parkId, DateTimeUtil.convertYMD(new Date()));
            //查询停车场今天预约成功的预约记录
            List<ParkReserveRecordDTO> recordDTOList = parkReserveRecordDao.getTodayReserveRecordByReserveDate(parkId);

            if (reserveTemporaryRuleDTO != null && reserveTemporaryRuleDTO.getReserveSpace() > 0) {
                count = reserveTemporaryRuleDTO.getReserveSpace() - recordDTOList.size();
                count = count < 0 ? 0 : count;
            } else if (reserveTemporaryRuleDTO == null && parkInfoDTO != null && parkInfoDTO.getReserveParkSpace() > 0) {
                count = parkInfoDTO.getReserveParkSpace();
                if (recordDTOList != null && recordDTOList.size() > 0) {
                    count = count - recordDTOList.size();
                    count = count < 0 ? 0 : count;
                }
            }
            parkInfoDTO.setReserveParkSpace(count);
        }
        return parkInfoDTO;
    }


}
